package com.yupi.oj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yupi.oj.model.dto.question.JudgeCase;
import com.yupi.oj.model.dto.question.JudgeConfig;
import com.yupi.oj.model.dto.questionsubmit.JudgeInfo;
import com.yupi.oj.model.entity.Question;
import com.yupi.oj.model.enums.JudgoInfeMessageEnum;

import java.util.List;

public class DefaultJudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {

        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        Question question = judgeContext.getQuestion();
        Long memory = judgeInfo.getMemory();
        Long time = judgeInfo.getTime();
        JudgeInfo judgeInfo1 = new JudgeInfo();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();

        JudgoInfeMessageEnum waiting = JudgoInfeMessageEnum.ACCEPTED;
        if(outputList.size() != inputList.size()){
            waiting = JudgoInfeMessageEnum.WRONG_ANSWER;
            judgeInfo1.setMessage(waiting.getValue());
            return judgeInfo1;
        }
        for(int i = 0;i<inputList.size();i++ ){
            JudgeCase judgeCase1 = judgeCaseList.get(i);
            if(judgeCase1.getOutput().equals(outputList.get(i))){
                waiting = JudgoInfeMessageEnum.WRONG_ANSWER;
            }
        }

        String judgeConfig = question.getJudgeConfig();
        JudgeConfig judgeConfig1 = JSONUtil.toBean(judgeConfig, JudgeConfig.class);
        Long needMemory = judgeConfig1.getMemoryLimit();
        Long needTimeLimit = judgeConfig1.getTimeLimit();

        if(memory>needMemory){
            waiting=JudgoInfeMessageEnum.MEMORY;
            judgeInfo1.setMessage(waiting.getValue());
            return  judgeInfo1;
        }
        if(time>needTimeLimit){
            waiting = JudgoInfeMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfo1.setMessage(waiting.getValue());
            return  judgeInfo1;
        }


        judgeInfo1.setMessage(waiting.getValue());
        judgeInfo1.setMemory(memory);
        judgeInfo1.setTime(time);

        return judgeInfo1;
    }
}
